package com.xigege.netty.nettyComponent.eventLoopAndEventLoopGroup;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;


/**
 * @author huangxi
 * @class EventLoopClient
 * @description EventLoopClient
 * @create 2022-08-19 18:35
 */
public class EventLoopClient {
    public static void main(String[] args) throws InterruptedException {
        Channel channel = new Bootstrap()
                .group(new NioEventLoopGroup(2))
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder());
                    }
                }).connect(new InetSocketAddress("localhost", 6666))
                // 同步阻塞等待连接
                .sync()
                // 拿到channel
                .channel();
        // 发送数据
        // 使用debug的evaluate expression，用channel向server发送信息（channel.writeAndFlush()）
        // debug类型为线程，否则所有线程都将会被阻塞，消息也发送不了
        // 观察是否是一个线程在处理此信息
        System.out.println(channel);
    }
}
